package com.cdream.basic.algorithm.leetcode.string;

public class Question13 {
    int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    public int romanToInt(String s) {
        int res = 0;
        for (int i = 0; i < symbols.length; i++) {
            int value = values[i];
            String symbol = symbols[i];
            while (s.startsWith(symbol)){
                res += value;
                s = s.substring(symbol.length());
            }
            if (s.length()==0){
                break;
            }
        }
        return res;
    }
}
